package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.dao.DriverDao;
import com.edu.dao.Impl.DriverDaoImpl;
import com.edu.entity.Driver;
import com.edu.service.DriverService;
import com.edu.service.Impl.DriverServiceImpl;


/**
 * Servlet implementation class DriverServletAll
 */
@WebServlet("/DriverServletAll")
public class DriverServletAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByDriver(request,response);
		//修改
		}else if("2".equals(menu)) {
			DriverUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			driverAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			driverDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	protected void queryByDriver(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int dId = -1;
		int dNum = -1;
		int dCarTime = -1;
		if(!("".equals(request.getParameter("dId")))){
			dId = Integer.parseInt(request.getParameter("dId"));
		}
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		if(!("".equals(request.getParameter("dNum")))){
			dNum = Integer.parseInt(request.getParameter("dNum"));
		}
		String dSex = request.getParameter("dSex");
		String dPhone = request.getParameter("dPhone");
		if(!("".equals(request.getParameter("dCarTime")))){
			dCarTime = Integer.parseInt(request.getParameter("dCarTime"));
		}
		Driver driver = new Driver(dId,dJob,dName,dNum,dSex,dCarTime,dPhone);
		DriverService driverService = new DriverServiceImpl();
		List<Driver> list = driverService.selectByDriver(driver);
		request.setAttribute("list",list);
		request.getRequestDispatcher("Driver.jsp").forward(request, response);
	}
	
	protected void DriverUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int dId = Integer.parseInt(request.getParameter("dId"));
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		int dNum = Integer.parseInt(request.getParameter("dNum"));
		String dSex = request.getParameter("dSex");
		int dCarTime = Integer.parseInt(request.getParameter("dCarTime"));
		String dPhone = request.getParameter("dPhone");
		
		Driver driver = new Driver(dId,dJob,dName,dNum,dSex,dCarTime,dPhone);
		DriverService driverService = new DriverServiceImpl();
		
		driverService.updateDriver(driver);
		response.sendRedirect("Driver.jsp");
	}
	
	protected void driverAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		int dNum = Integer.parseInt(request.getParameter("dNum"));
		String dSex = request.getParameter("dSex");
		int dCarTime = Integer.parseInt(request.getParameter("dCarTime"));
		String dPhone = request.getParameter("dPhone");
		

		Driver driver = new Driver(dJob,dName,dNum,dSex,dCarTime,dPhone);
		DriverService driverService = new DriverServiceImpl();
		
		driverService.addDriver(driver);
		response.sendRedirect("Driver.jsp");
	}
	
	protected void driverDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selectdId");
		DriverService driverService = new DriverServiceImpl();
		
		driverService.deleteDriverAll(ids);
		response.sendRedirect("Driver.jsp");
		
		
	}
}
